﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.OleDb;
using SmartLib;


/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection
{
   // private string connectionString;
    public string ConnectionString
    {
        get
        {
            if (HttpRuntime.Cache["Connection"] == null)
            {
                SqlConnection con = new SqlConnection("Data Source=C6E9961739FB407;Initial Catalog=QLMT;Integrated Security=True");
                HttpRuntime.Cache["Connection"] = con.ConnectionString;
                return con.ConnectionString;
            }
            else
            {
                return HttpRuntime.Cache["Connection"].ToString();
            }
        }
    }

    public void test() { }

    public static DataTable Truyvan_TraVe_Table_co_Thamso(string strSQL, SqlParameter[] thamso)
    {
        
        try
        {
            SqlConnection con = new SqlConnection("Data Source=C6E9961739FB407;Initial Catalog=QLMT;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(strSQL, con);
            for (int i = 0; i < thamso.Length; i++)
            {
                cmd.Parameters.Add(thamso[i]);
            }
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
            con.Close();
            con.Dispose();
        }
        catch (Exception e)
        {
            return null;
        }

    }
    public static int Add_tbl_KetQuaTinhToan(tbl_KetQuaTinhToan obj)
    {

        SqlConnection myCon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand myCmd = new SqlCommand("sp_Add_tbl_KetQuaTinhToan", myCon);
        myCmd.CommandType = System.Data.CommandType.StoredProcedure;

        myCmd.Parameters.Add("@p_CongXaId", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_DotGiamSatId", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_LuuLuong", System.Data.SqlDbType.Float);
        myCmd.Parameters.Add("@p_ChiTieu", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_NongDo", System.Data.SqlDbType.Float);



        if (obj._CongXaIdNULL)
            myCmd.Parameters["@p_CongXaId"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_CongXaId"].Value = obj.mCongXaId;

        if (obj._DotGiamSatIdNULL)
            myCmd.Parameters["@p_DotGiamSatId"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_DotGiamSatId"].Value = obj.mDotGiamSatId;

        if (obj._LuuLuongNULL)
            myCmd.Parameters["@p_LuuLuong"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_LuuLuong"].Value = obj.mLuuLuong;

        if (obj._ChiTieuNULL)
            myCmd.Parameters["@p_ChiTieu"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_ChiTieu"].Value = obj.mChiTieu;

        if (obj._NongDoNULL)
            myCmd.Parameters["@p_NongDo"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_NongDo"].Value = obj.mNongDo;

        SqlParameter pIdOut = new SqlParameter("@p_Id_out", System.Data.SqlDbType.Int);
        pIdOut.Direction = ParameterDirection.Output;
        myCmd.Parameters.Add(pIdOut);

        int ErrorCode = 0;
        try
        {
            myCon.Open();
            myCmd.ExecuteNonQuery();
            obj.mIdOut = (pIdOut.Value == null) ? "" : (string)pIdOut.Value.ToString();
        }
        catch (Exception e) { string ErrorStr = e.Message; ErrorCode = 1; }
        return ErrorCode;
    }
    public static int Update_tbl_KetQuaTinhToan(tbl_KetQuaTinhToan obj)
    {
        SqlConnection myCon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand myCmd = new SqlCommand("sp_Update_tbl_KetQuaTinhToan", myCon);
        myCmd.CommandType = System.Data.CommandType.StoredProcedure;

        myCmd.Parameters.Add("@p_Id", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_CongXaId", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_DotGiamSatId", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_LuuLuong", System.Data.SqlDbType.Float);
        myCmd.Parameters.Add("@p_ChiTieu", System.Data.SqlDbType.Int);
        myCmd.Parameters.Add("@p_NongDo", System.Data.SqlDbType.Float);


        if (obj._IdNULL)
            myCmd.Parameters["@p_Id"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_Id"].Value = obj.mId;

        if (obj._CongXaIdNULL)
            myCmd.Parameters["@p_CongXaId"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_CongXaId"].Value = obj.mCongXaId;

        if (obj._DotGiamSatIdNULL)
            myCmd.Parameters["@p_DotGiamSatId"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_DotGiamSatId"].Value = obj.mDotGiamSatId;

        if (obj._LuuLuongNULL)
            myCmd.Parameters["@p_LuuLuong"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_LuuLuong"].Value = obj.mLuuLuong;

        if (obj._ChiTieuNULL)
            myCmd.Parameters["@p_ChiTieu"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_ChiTieu"].Value = obj.mChiTieu;

        if (obj._NongDoNULL)
            myCmd.Parameters["@p_NongDo"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_NongDo"].Value = obj.mNongDo;

        int ErrorCode = 0;
        try
        {
            myCon.Open();
            myCmd.ExecuteNonQuery();
        }
        catch (Exception e) { string ErrorStr = e.Message; ErrorCode = 1; }
        return ErrorCode;
    }
    public static int Delete_tbl_KetQuaTinhToan(tbl_KetQuaTinhToan obj)
    {
        SqlConnection myCon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand myCmd = new SqlCommand("sp_Update_tbl_KetQuaTinhToan", myCon);
        myCmd.CommandType = System.Data.CommandType.StoredProcedure;

        myCmd.Parameters.Add("@p_Id", System.Data.SqlDbType.Int);

        if (obj._IdNULL)
            myCmd.Parameters["@p_Id"].Value = DBNull.Value;
        else
            myCmd.Parameters["@p_Id"].Value = obj.mId;


        int ErrorCode = 0;
        try
        {
            myCon.Open();
            myCmd.ExecuteNonQuery();
        }
        catch (Exception e) { string ErrorStr = e.Message; ErrorCode = 1; }
        return ErrorCode;
    }
  
}
